package com.sc.demo.enumtype;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

/**    
 *     
 * 项目名称：core    
 * 类名称：ReturnCodeEnum    
 * 类描述：  系统返回状态码  
 * 创建人：huan    
 * 创建时间：2016年3月16日 下午2:01:33    
 * 修改人：huan    
 * 修改时间：2016年3月16日 下午2:01:33    
 * 修改备注：    
 * @version 1.0
 *     
 */
public enum ReturnCodeEnum {
	
	C200_							(2000,"SUCESS!"),
	C202_ACCOUNT_SERVER_ERROR		(2020,"帐号服务器查询失败，请过一段时间重试"),
	C202_CANNOT_CREATE_APP_FOLDER	(2021,"不能创建用户目录，请重新获取ACCESS TOKEN"),
	C202_LOGIN_FAIL					(2022,"登陆失败，可能用户密码不对或者无此帐号"),
	C202_WRONG_VERIFICATION_CODE	(2023,"注册验证码错误或者失效"),
	C202_COMMIT_FAIL				(2024,"上传失败，请过一段时间重试"),
	C202_BAD_OPENID					(2025,"不能用此OPENID 注册"),
	C202_DUPLICATE_PHONEE			(2026,"该手机号已注册"),
	C202_DUPLICATE_PHONEE_MGM		(2027,"你已经是趣现用户，请直接下载APP开始信用卡取现"),
	C400_BAD_PARAMETERS				(4000,"参数不正确"),
	C400_BAD_REQUEST				(4001,"请求URL、参数不对"),
	C400_NO_SUCH_API_IMPLEMENTED	(4002,"无此API"),
	C401_BAD_SIGNATURE				(4013,"签名不正确"),
	C401_REQUEST_EXPIRED			(4014,"请求过期（TIMESTAMP 或者NONCE不正确）"),
	C401_BAD_CONSUMER_KEY			(4015,"没有这个CONSUMER KEY （你确定申请了应用？）"),
	C401_NOT_SUPPORTED_AUTH_MODE	(4016,"你不能使用这种授权模式"),
	C401_AUTHORIZATION_EXPIRED		(4017,"用户的授权过期，请重新获取ACCESS_TOKEN"),
	C401_API_DAILY_LIMIT			(4018,"您今天调用接口次数太多了吧"),
	C401_NO_RIGHT_TO_CALL_THIS_API	(4019,"无权限调用此API"),
	C401_REUSED_NONCE				(40110,"NONCE被重用了"),
	C401_BAD_VERIFIER				(40111,"VERIFIER错误"),
	C403_FILE_EXIST					(4030,"同名文件（夹）已经存在"),
	C403_FORBIDDEN					(4031,"无权限访问，或者无权限进行此操作"),
	C404_FILE_NOT_EXIST				(4040,"文件夹不存在，父目录不存在"),
	C404_MSG_NOT_FOUND				(4041,"查无此数据"),
	C405_METHOD_NOT_SUPPORT			(4050,"不支持此方法"),
	C406_TOO_MANY_FILES				(4060,"一次操作太多文件"),
	C413_FILE_TOO_LARGE				(4130,"文件大小超过能上传的上限"),
	C500_SERVER_ERROR				(5000,"服务器错误，应该是我们遇到BUG了"),
	C507_OVER_SPACE					(5070,"用户空间已满");
	
	private int code;
	private String message;
	@SuppressWarnings("unused")
	private String date;
	private Map<String , Object> data;


	public Map<String, Object> getData() {
		return data;
	}

	public ReturnCodeEnum setData(Map<String, Object> data) {
		this.data = data;
		return this;
	}

	public String getDate() {
		return System.currentTimeMillis()+"";
	}

	public ReturnCodeEnum setDate(String date) {
		this.date = date;
		return this;
	}
	
	
	private ReturnCodeEnum ( int code , String message ){
		this.code = code;
		this.message = message;
	}

	public int getCode() {
		return code;
	}

	public ReturnCodeEnum setCode(int code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public ReturnCodeEnum setMessage(String message) {
		this.message = message;
		return this;
	}

	public static ReturnCodeEnum getEnumByName(int status) {
		ReturnCodeEnum[] enums = ReturnCodeEnum.values();
		for(ReturnCodeEnum enumtype:enums) {
			if(enumtype.getCode()==status) {
				return enumtype;
			}
		}
		return C400_BAD_REQUEST;
	}
	
	public String toSimpleString() {
		Map<String , Object> map = new HashMap<>();
		map.put("code", code);
		map.put("message", message);
		map.put("date", getDate());
 		return JSON.toJSONString(map) ;
	}
	
	public Map<String , Object> toSimpleMap() {
        Map<String , Object> map = new HashMap<>();
        map.put("code", code);
        map.put("message", message);
        map.put("date", getDate());
        map.put("data", this.getData());
        this.setData(null);
        return map ;
    }
	
	public ReturnCodeEnum add(String key , Object value ) {
        Map<String , Object> data = this.getData()==null ?
        			new HashMap<>():this.getData();
        data.put(key, value);
        this.setData(data);
        return this ;
    }
	
	public Map<String , Object> add2Map(String key , Object value ) {
        Map<String , Object> map = this.toSimpleMap();
        Map<String , Object> data = new HashMap<>();
        data.put(key, value);
        map.put("data", data);
        return map ;
    }
	
	public String add2String(String key , Object value) {
        Map<String , Object> map = this.toSimpleMap();
        Map<String , Object> data = new HashMap<>();
        data.put(key, value);
        map.put("data", data);
        return JSON.toJSONString(map) ;
    }
	
	public static void main(String[] args) {
		System.out.println(ReturnCodeEnum.C200_.toSimpleString());
		System.out.println(ReturnCodeEnum.C200_.getCode());
	}
	
}
